//
//  SocialCell.swift
//  BaseSwift
//
//  Created by gh on 2019/10/9.
//  Copyright © 2019 ghwang. All rights reserved.
//

import UIKit

class SocialCell: UITableViewCell {

    let titleLab = UILabel()
    let photo = UIImageView()
    let contLab = UILabel()
    
    let icon = UILabel()
    let nickLab = UILabel()
    let timeLab = UILabel()
    var bottV:UIView?
    let statusLab = UILabel()
        
    let containerView = UIView()
    func setModel(mod:TopPic) {
        
        if mod.userPostingImgList.count == 0 {
            
            titleLab.text = mod.title
            let _ = titleLab.sd_layout()?.leftSpaceToView(containerView,16)!.rightSpaceToView(containerView,16)!.topSpaceToView(containerView,16)!.heightIs(20)
            contLab.text = mod.content
            
            let _ = contLab.sd_layout()?.leftSpaceToView(containerView,16)!.rightSpaceToView(containerView,16)!.topSpaceToView(titleLab,16)!.autoHeightRatio(0)
            contLab.setMaxNumberOfLinesToShow(2)
            photo.isHidden = true
            

            
            
        }else{
            
            photo.isHidden = false
            titleLab.text = mod.title
            let _ = titleLab.sd_layout()?.leftSpaceToView(containerView,16)!.rightSpaceToView(containerView,135)!.topSpaceToView(containerView,16)!.autoHeightRatio(0)
            titleLab.setMaxNumberOfLinesToShow(3)
            
            let _ = photo.sd_layout()?.widthIs(80)!.heightIs(80)!.rightSpaceToView(containerView,16)!.topSpaceToView(containerView,20)
            photo.backgroundColor = UIColor.cccColor
            photo.sd_cornerRadius = 8
            
            contLab.text = mod.content
            
            let _ = contLab.sd_layout()?.leftSpaceToView(containerView,16)!.rightSpaceToView(containerView,16)!.topSpaceToView(photo,16)!.autoHeightRatio(0)
            contLab.setMaxNumberOfLinesToShow(2)
            

        }
        
        if mod.status == "INVALID" {
            
            statusLab.isHidden = false

            statusLab.text = "待审核"
            statusLab.sd_cornerRadius = 3
            let _ = statusLab.sd_layout()?.leftSpaceToView(containerView,16)!.widthIs(60)!.topSpaceToView(containerView,16)!.heightIs(20)
            let _ = titleLab.sd_layout()?.leftSpaceToView(statusLab,5)!.rightSpaceToView(containerView,16)!.topSpaceToView(containerView,16)!.heightIs(20)
        }else{
            
            statusLab.isHidden = true
            let _ = titleLab.sd_layout()?.leftSpaceToView(containerView,16)!.rightSpaceToView(containerView,16)!.topSpaceToView(containerView,16)!.heightIs(20)
        }
        
        if bottV != nil {
            
            bottV?.removeFromSuperview()
        }
        bottV = self.createBottomView(mod: mod)
        containerView.addSubview(bottV!)
        let _ =  bottV!.sd_layout()?.widthRatioToView(containerView,1)?.heightIs(35)?.bottomSpaceToView(containerView,15)
        

        
        
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        self.contentView.addSubview(containerView)
        self.backgroundColor = UIColor.backgroundColor
        containerView.backgroundColor = UIColor.white
        self.selectionStyle = .none

        let _ = containerView.sd_layout()?.spaceToSuperView(UIEdgeInsets(top: 10, left: 0, bottom: 0, right: 0))
        containerView.sd_addSubviews([statusLab,titleLab,contLab,photo])
        titleLab.font = UIFont.systemFont(ofSize: 18, weight: UIFont.Weight(rawValue: 0.2))
        titleLab.textColor = UIColor.textTitleColorBlack
        
        contLab.textColor = UIColor.textTitleColor
        contLab.font = UIFont.commonFontSmaller
        
        
        statusLab.textAlignment =  .center
        statusLab.font = UIFont.smallerFont
        statusLab.backgroundColor = UIColor.GP_Orange_Color
        statusLab.textColor = UIColor.white

        
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    func createBottomView(mod:TopPic) -> UIView{
           
           let bottomView = UIView()
           let userIcon = UIImageView()
        
           let nickNameLab = UILabel()
                  containerView.addSubview(bottomView)
                  let _ =  bottomView.sd_layout()?.widthRatioToView(containerView,1)?.heightIs(35)?.bottomSpaceToView(containerView,5)
                  
                  bottomView.addSubview(userIcon)
        userIcon.sd_cornerRadius = 10
                  let _  = userIcon.sd_layout()?.widthIs(20)?.heightIs(20)?.centerYEqualToView(bottomView)?.leftSpaceToView(bottomView,16)
                  
        userIcon.sd_setImage(with: URL(string:mod.userHead ?? "http"), placeholderImage: UIImage(named: "默认头像"), options: .lowPriority) { (A, B, C, D) in
            
        }
                  bottomView.addSubview(nickNameLab)
                  nickNameLab.font = UIFont.smallerLessthanFont
                  nickNameLab.textColor = UIColor.textPlaceHolderColor
        if mod.userName?.phoneValidate == true {
            
            nickNameLab.text = mod.userName!.mobileSecure(mobile: mod.userName!)

        }else{
            
            nickNameLab.text = mod.userName

        }
        
                  let _  = nickNameLab.sd_layout()?.heightIs(20)?.centerYEqualToView(bottomView)?.leftSpaceToView(userIcon,5)
                  nickNameLab.setSingleLineAutoResizeWithMaxWidth(155)
                  
                  let commentBtn = UIButton()
                  commentBtn.setImage(UIImage(named: "评论1"), for: .normal)
                  commentBtn.titleLabel?.font = UIFont.smallerLessthanFont
                  commentBtn.setTitleColor(UIColor.textPlaceHolderColor, for: .normal)
        commentBtn.setTitle(mod.sayNum ?? "0", for: .normal)
                  bottomView.addSubview(commentBtn)
                  let _ = commentBtn.sd_layout()?.rightSpaceToView(bottomView,5)?.centerYEqualToView(bottomView)?.widthIs(55)
                  
                  
                  let _ = commentBtn.imageView?.sd_layout()?.widthIs(15)?.heightIs(15)?.leftSpaceToView(commentBtn,0)?.centerYEqualToView(commentBtn)
                  
                  let _ = commentBtn.titleLabel?.sd_layout()?.heightIs(15)?.leftSpaceToView(commentBtn.imageView,5)?.widthIs(45)?.centerYEqualToView(commentBtn)
                  commentBtn.titleLabel?.textAlignment = .left
                  
                  let favBtn = UIButton()
                  favBtn.setImage(UIImage(named: "eye"), for: .normal)
                  favBtn.titleLabel?.font = UIFont.smallerLessthanFont
                  favBtn.setTitleColor(UIColor.textPlaceHolderColor, for: .normal)
        favBtn.setTitle(mod.viewNum ?? "0", for: .normal)
                  bottomView.addSubview(favBtn)
                  let _ = favBtn.sd_layout()?.rightSpaceToView(commentBtn,30)?.centerYEqualToView(bottomView)?.widthIs(55)
                  
                  
                  let _ = favBtn.imageView?.sd_layout()?.widthIs(22)?.heightIs(22)?.rightSpaceToView(favBtn,0)?.centerYEqualToView(favBtn)
                  
                  let _ = favBtn.titleLabel?.sd_layout()?.heightIs(15)?.leftSpaceToView(favBtn.imageView,5)?.widthIs(45)?.centerYEqualToView(favBtn)
                  favBtn.titleLabel?.textAlignment = .left
           
                  return bottomView
           
       }
    
}
